package com.niuniu.rushdorm.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.niuniu.rushdorm.mapper.GroupsMapper;
import com.niuniu.rushdorm.pojo.entity.Groups;
import com.niuniu.rushdorm.config.RabbitMQConfig;
import com.niuniu.rushdorm.mapper.BedMapper;
import com.niuniu.rushdorm.mapper.BuildingMapper;
import com.niuniu.rushdorm.pojo.entity.Bed;
import com.niuniu.rushdorm.exception.ReportedException;
import com.niuniu.rushdorm.mapper.GroupsUserMapper;
import com.niuniu.rushdorm.mapper.OrderMapper;
import com.niuniu.rushdorm.pojo.entity.Order;
import com.niuniu.rushdorm.pojo.entity.Student;
import com.niuniu.rushdorm.pojo.vo.UserVO;
import com.niuniu.rushdorm.service.OrderService;
import com.niuniu.rushdorm.util.StatusEnum;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    BedMapper bedMapper;
    @Autowired
    GroupsMapper groupsMapper;
    @Autowired
    GroupsUserMapper groupsUserMapper;
    @Autowired
    BuildingMapper buildingMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;

//    @Override
//    public int createOrder(UserVO userVO, Integer groupId, Integer buildingId) {
//        Student student = userVO.getStudent();
//
//        // 根据uid，在床位表里查该学生是否已经选到宿舍，选到则抛异常
//        Long uid = student.getUid();
//        LambdaQueryWrapper<Bed> bedQuery = new LambdaQueryWrapper<>();
//        bedQuery.eq(Bed::getUid, uid);
//        if (bedMapper.selectCount(bedQuery) == 1)
//            throw ReportedException.build(StatusEnum.ORDER_ALREADY_HAS_DORM);
//
//        // 创建订单
//        Order order = new Order();
//        order.setSubmitTime(System.currentTimeMillis() / 1000);
//        order.setUid(student.getUid());
//        order.setGroupId(groupId);
//        order.setBuildingId(buildingId);
//        order.setCreateTime(System.currentTimeMillis() / 1000);
//        orderMapper.insert(order);
//
//        // 订单连同该学生的性别，一起发送至消息队列进行订单处理
//        Map<String, Object> message = new HashMap<>();
//        message.put("order", order);
//        message.put("gender", userVO.getGender());
//        rabbitTemplate.convertAndSend(RabbitMQConfig.QUEUE_NAME, JSON.toJSONString(message));
//
//        // 先把订单号返回给用户
//        return order.getId();
//    }

    @Override
    public Map<String, List<Map<String, Object>>> getOrderList(UserVO userVO) {
        Student student = userVO.getStudent();

        // 根据uid查该学生的所有订单
        Long uid = student.getUid();
        LambdaQueryWrapper<Order> orderQuery = new LambdaQueryWrapper<>();
        orderQuery.eq(Order::getUid, uid);
        List<Order> orderList = orderMapper.selectList(orderQuery);

        List<Map<String, Object>> result = new ArrayList<>();
        for (Order order : orderList) {
            // 先根据订单中的组号、楼号，查组名、楼名
            Integer groupId = order.getGroupId(), buildingId = order.getBuildingId();
            String groupName = "未组队";
            if (groupId != 0) {
                Groups groups = groupsMapper.selectById(groupId);
                groupName = groups.getName();
            }
            String buildingName = buildingMapper.GetBuildingById(buildingId).getName();

            // 封装返回结果
            Map<String, Object> map = new HashMap<>();
            map.put("order_id", order.getId());
            map.put("group_name", groupName);
            map.put("building_name", buildingName);
            map.put("submit_time", order.getSubmitTime());
            map.put("result_content", order.getResult());
            map.put("status", order.getStatus());
            result.add(map);
        }

        Map<String, List<Map<String, Object>>> response = new HashMap<>();
        response.put("rows", result);
        return response;
    }

    @Override
    public Map<String, Object> getOrderInfo(Integer orderId) {
        Order order = orderMapper.selectById(orderId);
        Map<String, Object> map = new HashMap<>();
        map.put("status", order.getStatus());
        map.put("room_id", order.getRoomId());
        return map;
    }
}
